package cn.lovemmd.model.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import cn.lovemmd.model.entity.Model;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 模型Mapper接口
 * 
 * @author TLS
 * @since 2024-01-01
 */
@Mapper
public interface ModelMapper extends BaseMapper<Model> {

    /**
     * 分页查询模型列表
     * 
     * @param page 分页参数
     * @param search 搜索关键字
     * @param occupation 职业筛选
     * @return 分页结果
     */
    IPage<Model> selectModelPage(Page<Model> page, 
                                @Param("search") String search, 
                                @Param("occupation") String occupation);

    /**
     * 根据职业统计模型数量
     * 
     * @return 职业统计列表
     */
    List<Object> selectOccupationStats();

    /**
     * 根据ID查询模型详情
     * 
     * @param id 模型ID
     * @return 模型详情
     */
    Model selectModelById(@Param("id") Long id);

    /**
     * 批量删除模型
     * 
     * @param ids 模型ID列表
     * @return 删除数量
     */
    int deleteBatchIds(@Param("ids") List<Long> ids);

    /**
     * 最近新增模型
     */
    @Select("SELECT m_id AS id, m_name AS name, create_time FROM model_list ORDER BY create_time DESC LIMIT #{limit}")
    List<Object> selectRecentModels(@Param("limit") int limit);

    /**
     * 根据职业查询模型列表
     */
    @Select("SELECT * FROM model_list WHERE m_occupation = #{occupation} AND deleted = 0 ORDER BY m_download_count DESC")
    List<Model> selectByOccupation(@Param("occupation") String occupation);

    /**
     * 根据分类查询模型列表
     */
    @Select("SELECT * FROM model_list WHERE m_category = #{category} AND deleted = 0 ORDER BY m_download_count DESC")
    List<Model> selectByCategory(@Param("category") String category);

    /**
     * 搜索模型
     */
    @Select("SELECT * FROM model_list WHERE (m_name LIKE CONCAT('%', #{keyword}, '%') OR m_description LIKE CONCAT('%', #{keyword}, '%')) AND deleted = 0 ORDER BY m_download_count DESC")
    List<Model> searchByKeyword(@Param("keyword") String keyword);

    /**
     * 获取热门模型
     */
    @Select("SELECT * FROM model_list WHERE deleted = 0 ORDER BY m_download_count DESC LIMIT #{limit}")
    List<Model> selectTopModels(@Param("limit") int limit);

    /**
     * 统计模型总数
     */
    @Select("SELECT COUNT(*) FROM model_list WHERE deleted = 0")
    Long countTotalModels();

    /**
     * 获取职业统计
     */
    @Select("SELECT m_occupation as occupation, COUNT(*) as count FROM model_list WHERE deleted = 0 GROUP BY m_occupation")
    List<Object> getOccupationStats();

    /**
     * 获取分类统计
     */
    @Select("SELECT m_category as category, COUNT(*) as count FROM model_list WHERE deleted = 0 GROUP BY m_category")
    List<Object> getCategoryStats();

    /**
     * 增加下载次数
     */
    @Select("UPDATE model_list SET m_download_count = m_download_count + 1 WHERE m_id = #{modelId}")
    int incrementDownloadCount(@Param("modelId") Long modelId);

    /**
     * 增加浏览次数
     */
    @Select("UPDATE model_list SET m_view_count = m_view_count + 1 WHERE m_id = #{modelId}")
    int incrementViewCount(@Param("modelId") Long modelId);
}
